/*************************************************************************
    > File Name: 6.pthread_detach.c
    > Author: Ykk
    > Mail: 3393579535@qq.com 
    > Created Time: 2023年05月10日 星期三 20时21分48秒
 ************************************************************************/

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#include<unistd.h>
#include<pthread.h>

void *fun(void *arg)
{
	int i = 0;
	for(i = 0;i < 5;i++)
	{
		printf("pthread do working...%d\n",i);
		sleep(1);
	}

	//等价与renturn线程退出
	pthread_exit(NULL);
	//return NULL;
}
int main()
{
	int ret = -1;
	pthread_t tid = -1;

	//创键一个线程
	ret = pthread_create(&tid,NULL,fun,NULL);
	if(0 != ret)
	{
		printf("pthread_create failed...\n");
		return 1;
	}
	printf("main thread...tid:%lu\n",pthread_self());

	//设置线程分离
	ret = pthread_detach(tid);
	if(0 != ret)
	{
		printf("pthread_detach failed...\n");
		return 1;
	}
	
	//测试分离状态的线程是否可以被join
	ret = pthread_join(tid,NULL);
	if(0 != ret)
	{
		printf("pthread_join failed...\n");
	}
	else
	{
		printf("pthread_join ok\n");
	}

	printf("按下回车键主线程退出...\n");
	getchar();

	return 0;
}
